/*
 * Class name: PlayerValidator.java
 * Version: 1.0
 * Date: 9.10. 2013
 */
package cz.muni.fi.pa165.soccergamesarchive.validators;

import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.Player;

import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCOMPLETE_RECORD_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCORRECT_ID_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.NO_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.NULL_VALUE_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.RUNTIME_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.createErrorMessage;

/**
 *
 * @author Michal Ďuriš
 */
public class PlayerValidator {

    public static int validatePlayer(Player player, ValidatorTest... tests)
            throws RuntimeException {

        for (ValidatorTest test : tests) {
            switch (test) {
                case NULL_TEST:
                    executeNullTest(player);
                    break;
                case ID_TEST:
                    executeIDTest(player);
                    break;
                case CORRECT_RECORD_TEST:
                    executeCorrectRecordTest(player);
                    break;
                case FULL_TEST:
                    executeFullTest(player);
                    break;
                default:
                    //@TODO: Maybe change it to the logger...
                    System.err.println("WAW...Unidentified test");
                    return RUNTIME_ERROR;
            }
        }

        return NO_ERROR;
    }

    private static void executeNullTest(Player player) throws
            IllegalArgumentException {

        if (player == null) {
            throw new IllegalArgumentException(createErrorMessage(
                    PlayerValidator.class.getName(), NULL_VALUE_ERROR));
        }
    }

    private static void executeIDTest(Player player) throws
            IllegalArgumentException {

        if (player.getId() == null || player.getId() < 1) {

            throw new IllegalArgumentException(createErrorMessage(
                    PlayerValidator.class.getName(), INCORRECT_ID_ERROR));
        }
    }

    private static void executeCorrectRecordTest(Player player) throws
            IllegalArgumentException {

        if ((player.getFirstName() == null || player.getFirstName().equals(""))
                || (player.getLastName() == null || player.getLastName().equals(
                        ""))
                || (player.getTeam() == null) || (player.getShirtNumber() == 0)) {

            throw new IllegalArgumentException(createErrorMessage(
                    PlayerValidator.class.getName(), INCOMPLETE_RECORD_ERROR));
        }
    }

    private static void executeFullTest(Player player) throws
            IllegalArgumentException {

        executeNullTest(player);
        executeIDTest(player);
        executeCorrectRecordTest(player);
    }

}
